package com.um.jdy.platform.device.manger;

import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.device.service.PileService;
import com.um.jdy.platform.device.entity.vo.DeviceMonthItemVO;
import com.um.jdy.platform.device.entity.vo.DeviceOverviewVO;
import com.um.jdy.platform.device.entity.vo.DeviceRateItemVO;
import com.um.jdy.platform.device.entity.vo.DeviceTypeItemVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 管理类：充电枪表
 */
@Component
public class DeviceOverviewManager extends BaseManager<PileService> {

    /**
     * 设备概览图表
     * @return
     */
    public DeviceOverviewVO getOverviewData(){

        Date date = new Date();
        String comId = getComId();
        String year = DateUtil.format(date,"yyyy");

        DeviceOverviewVO deviceOverviewVO = new DeviceOverviewVO();

        Integer onlineCount = service.getCountBySqlName("get_device_overview_online_count",new Object[]{comId}).intValue();
        Integer offlineCount = service.getCountBySqlName("get_device_overview_offline_count",new Object[]{comId}).intValue();
        Integer monthIncrease = service.getCountBySqlName("get_device_overview_month_increase",new Object[]{DateUtil.format(date,"yyyy-MM"),comId}).intValue();
        Integer yearIncrease = service.getCountBySqlName("get_device_overview_year_increase",new Object[]{year,comId}).intValue();

        List<DeviceMonthItemVO> deviceMonthList = service.getViewModelList("get_device_overview_month_increase_list",DeviceMonthItemVO.class,new Object[]{year,comId});

        for(int index = 1;index <= 12;index ++){
            String month = year+"-"+(index < 10 ? "0"+index : index);

            boolean hasFind = false;
            for(DeviceMonthItemVO item : deviceMonthList){
                if(item.getMonth().equals(month)){
                    hasFind = true;
                    break;
                }
            }

            if(!hasFind){
                DeviceMonthItemVO newItem = new DeviceMonthItemVO();
                newItem.setMonth(month);
                newItem.setNum(0);
                deviceMonthList.add(index - 1,newItem);
            }
        }

        List<DeviceTypeItemVO> deviceTypeList = service.getViewModelList("get_device_overview_type_list",DeviceTypeItemVO.class,new Object[]{comId});
        Integer sum  = deviceTypeList.stream().mapToInt(DeviceTypeItemVO::getNum).sum();
        for(DeviceTypeItemVO item : deviceTypeList){
            item.setRadio(item.getNum() * 1.0 / sum * 100);
        }

        Date beginDate = DateUtil.parse(year+"-01-01","yyyy-MM-dd");
        long duration = DateUtil.betweenDay(beginDate,date,true) * 24 * 3600;
        List<DeviceRateItemVO> deviceRateList = service.getViewModelList("get_device_type_rate_list",DeviceRateItemVO.class,new Object[]{comId,year,duration});

        deviceOverviewVO.setOnlineCount(onlineCount);
        deviceOverviewVO.setOfflineCount(offlineCount);
        deviceOverviewVO.setMonthIncrease(monthIncrease);
        deviceOverviewVO.setYearIncrease(yearIncrease);

        deviceOverviewVO.setDeviceMonthList(deviceMonthList);
        deviceOverviewVO.setDeviceTypeList(deviceTypeList);
        deviceOverviewVO.setDeviceRateList(deviceRateList);

        return deviceOverviewVO;
    }

}
